using System;
using System.ComponentModel.Composition;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using SeventhSenseSystems.HighFlyingPm.Business.Entities;
using SeventhSenseSystems.HighFlyingPm.Business.Models;

namespace WebBusiness.Controllers
{   
	[Export("issue",typeof(IController))]
	[PartCreationPolicy(CreationPolicy.NonShared)]
	public class IssueController : Controller
	{
		private readonly IProjectRepository projectRepository;
		private readonly IUserRepository userRepository;
		private readonly ITrackerRepository trackerRepository;
		private readonly IIssueRepository issueRepository;

		[ImportingConstructor]
		public IssueController(IProjectRepository projectRepository, IUserRepository userRepository, ITrackerRepository trackerRepository, IIssueRepository issueRepository)
		{
			this.projectRepository = projectRepository;
			this.userRepository = userRepository;
			this.trackerRepository = trackerRepository;
			this.issueRepository = issueRepository;
		}

		//
		// GET: /Issue/

		public ViewResult Index()
		{
			return View(issueRepository.AllIncluding(issue => issue.Project, issue => issue.Author, issue => issue.Tracker));
		}

		//
		// GET: /Issue/Details/5

		public ViewResult Details(System.Guid id)
		{
			return View(issueRepository.Find(id));
		}

		//
		// GET: /Issue/Create

		public ActionResult Create()
		{
			ViewBag.PossibleProjects = projectRepository.All;
			ViewBag.PossibleAuthors = userRepository.All;
			ViewBag.PossibleTrackers = trackerRepository.All;
			return View();
		} 

		//
		// POST: /Issue/Create

		[HttpPost]
		public ActionResult Create(Issue issue)
		{
			if (ModelState.IsValid) {
				issue.Id = Guid.NewGuid();
				issueRepository.Add(issue);
				issueRepository.Save();
				return RedirectToAction("Index");
			} else {
				ViewBag.PossibleProjects = projectRepository.All;
				ViewBag.PossibleAuthors = userRepository.All;
				ViewBag.PossibleTrackers = trackerRepository.All;
				return View();
			}
		}
		
		//
		// GET: /Issue/Edit/5
 
		public ActionResult Edit(System.Guid id)
		{
			ViewBag.PossibleProjects = projectRepository.All;
			ViewBag.PossibleAuthors = userRepository.All;
			ViewBag.PossibleTrackers = trackerRepository.All;
			 return View(issueRepository.Find(id));
		}

		//
		// POST: /Issue/Edit/5

		[HttpPost]
		public ActionResult Edit(Issue issue)
		{
			if (ModelState.IsValid) {
				issueRepository.Edit(issue);
				issueRepository.Save();
				return RedirectToAction("Index");
			} else {
				ViewBag.PossibleProjects = projectRepository.All;
				ViewBag.PossibleAuthors = userRepository.All;
				ViewBag.PossibleTrackers = trackerRepository.All;
				return View();
			}
		}

		//
		// GET: /Issue/Delete/5
 
		public ActionResult Delete(System.Guid id)
		{
			return View(issueRepository.Find(id));
		}

		//
		// POST: /Issue/Delete/5

		[HttpPost, ActionName("Delete")]
		public ActionResult DeleteConfirmed(System.Guid id)
		{
			issueRepository.Delete(issueRepository.Find(id));
			issueRepository.Save();

			return RedirectToAction("Index");
		}

		protected override void Dispose(bool disposing)
		{
			if (disposing) {
				projectRepository.Dispose();
				userRepository.Dispose();
				trackerRepository.Dispose();
				issueRepository.Dispose();
			}
			base.Dispose(disposing);
		}
	}
}

